<?php
namespace Photo\Controller;
use  Common\Controller\PhotoController;
class UserController extends PhotoController {


    public function add_form_id()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if(! empty($input_data['app']) && ! empty($input_data['version'])
            && ! empty($input_data['token']) && ! empty($input_data['form_id']))
        {
            $where['token'] =   $input_data['token'];
            $field          =   array('user_id','expire_time','perfect_time','openid');
            $member_data    =   M('photo_member')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('','please login update',101);
            }

            $template_id    =   1;
            $date           =   date('Y-m-d');
            $form_type      =   1;
            $insert = array(
                'appid'=>'wxec5cdb888a5fcdda',
                'touser'=>$member_data['openid'],
                'template_id'=>$template_id,
                'page'=>'pages/index',
                'form_id'=>$input_data['form_id'],
                'form_type'=>$form_type,
                'status'=>'-1',
                'add_time'=>time(),
                'secret'=>'c7e47535ef8a22a6e2b0a5730a8f57ba',
                'createtime'=>$date,
                'user_id'=>$member_data['user_id']
            );
            M('photo_template_send')->data($insert)->add();


//            $where               = array();
//            $where['touser']     = $member_data['openid'];
//            $where['createtime'] = $date;
//            $where['status']     = '-1';
//            $ishave = M('photo_template_send')->field('wxopen_template_send_id')->where($where)->find();
//            if ($ishave)
//            {
//                M('photo_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
//            }
//            else
//            {
//                M('photo_template_send')->data($insert)->add();
//            }
        }
    }

    public function report_jt()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data) || $_POST)
        {
            if (! empty($_POST))
            {
                $input = json_encode($_POST);
            }

            $data               =   array();
            $data['module']     =   'user';
            $data['url']        =    $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            $data['log']        =    $input;
            $data['create_time']=   time();
            M('yao_log')->add($data);
        }
    }

    public function add_friend()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data) || $_POST)
        {
            if (! empty($_POST))
            {
                $input = json_encode($_POST);
            }

            $data               =   array();
            $data['module']     =   'user';
            $data['url']        =    $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            $data['log']        =    $input;
            $data['create_time']=   time();
            M('yao_log')->add($data);
        }
    }

    //完成
    public function update_user_info()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if(! empty($input_data['app']) && ! empty($input_data['version']) && ! empty($input_data['token']))
        {
            $where['token'] =   $input_data['token'];
            $field          =   array('user_id','expire_time','perfect_time');
            $member_data    =   M('photo_member')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('','please login update',101);
            }

            if ($member_data['expire_time'] < time())
            {
                $this->returnData('','please login',101);
            }

            $user_id          =   $member_data['user_id'];

            //更新用户信息
            $update_data                =   array();
            if (! empty($input_data['nickname']))
            {
                $update_data['nickname']    =   $input_data['nickname'];
            }

            if (! empty($input_data['avatar']))
            {
                $update_data['avatar']    =   $input_data['avatar'];
            }

            if (! empty($input_data['city']))
            {
                $update_data['city']    =   $input_data['city'];
            }

            if (! empty($input_data['country']))
            {
                $update_data['country']    =   $input_data['country'];
            }

            if (! empty($input_data['province']))
            {
                $update_data['province']    =   $input_data['province'];
            }

            if (! empty($input_data['country']))
            {
                $update_data['country']    =   $input_data['country'];
            }

            if (! empty($input_data['gender']))
            {
                $update_data['gender']    =   $input_data['gender'];
            }

            if (! empty($update_data) && ($member_data['perfect_time'] < time() || $member_data['perfect_time'] == 0) )
            {
                $update_data['update_time'] =   time();
                $update_data['perfect_time']=   time() + 7*24;
                $result     =   M('photo_member')->where(array('user_id'=>$user_id))->save($update_data); // 根据条件更新记录
            }

            $this->returnData('','ok',0);
        }
        else
        {
            $this->returnData('','please login not data',101);
        }
    }

    //完成
    public function user_info()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data['app']) && ! empty($input_data['version']) && ! empty($input_data['token']) )
        {
            $where['token'] =   $input_data['token'];
            $field          =   array('user_id','expire_time','nickname','avatar','token','token_end_time');
            $member_data    =   M('photo_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                if ($member_data['expire_time'] < time())
                {
                    $this->returnData('','please login',101);
                }

                $data   =   $member_data;
                $this->returnData('','ok',0);
            }
            else
            {
                $this->returnData('','please login',101);
            }
        }
    }

    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function login()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data['app']) && ! empty($input_data['version']) && ! empty($input_data['code']))
        {
            $code       =   $input_data['code'];
            $appid      =   'wxec5cdb888a5fcdda';
            $secret     =   'c7e47535ef8a22a6e2b0a5730a8f57ba';
            $share_uid  =   I('post.share_uid','','htmlspecialchars');

            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

            $res = $this->curlget($url);

            if (!$res['openid'])
            {
                $this->returnData($res,$msg='openid获取失败',$code=1001);
            }

            $api_token      =   array();
            $user_id        =   0;
            $data['openid'] =   $res['openid'];
            $where['openid']=   $data['openid'];
            $field          =   array('user_id','nickname','avatar','openid','login_num');
            $result         =   M('photo_member')->where($where)->field($field)->find();
            if (empty($result))
            {
                $insert_data                =   array();
                $insert_data['openid']      =   $data['openid'];
                $insert_data['create_time'] =   time();
                $insert_data['login_time']  =   time();
                $insert_data['session_key'] =   $res['session_key'];
                $insert_data['login_num']   =   1;
                $user_id                    =   M('photo_member')->add($insert_data);
            }
            else
            {
                $user_id                    =   $result['user_id'];
                $api_token['login_time']    =   time();
                $api_token['login_num']     =   $result['login_num'] + 1;
                $api_token['session_key']   =   $res['session_key'];
            }

            //生成token
            $module     =   'photo';
            $Controller =   'user';
            $Method     =   'login';
            $expire_time=   time() + 7*24*3600;
            $key        =   'yuanyi2018';
            $api_token['token']             =   md5($module.$Controller.$Method.$expire_time.$key.$user_id);
            $api_token['expire_time']       =   $expire_time;
            $api_token['update_time']       =   time();
            M('photo_member')->where(array('user_id'=>$user_id))->save($api_token); // 根据条件更新记录

            //绑定好友关系
            if (! empty($share_uid))
            {
                $this->make_friend($result['yao_member_id'] ,$share_uid);
            }

            $data['token']  =   $api_token['token'];
            $this->returnData($data,'ok',0);
        }
        else
        {
            $data   =   array();
            $this->returnData($data,'参数错误',1001);
        }
    }

    public function update_user()
    {
        $token                  =   I('post.token','','htmlspecialchars');
        $user_data              =   array();
        $user_data['avatar']    =   I('post.avatar','','htmlspecialchars');
        $user_data['city']      =   I('post.city','','htmlspecialchars');
        $user_data['gender']    =   I('post.gender','','htmlspecialchars');
        $user_data['language']  =   I('post.language','','htmlspecialchars');
        $user_data['nickname']  =   I('post.nickname','','htmlspecialchars');
        $user_data['province']  =   I('post.province','','htmlspecialchars');
        $user_data['country']   =   I('post.country','','htmlspecialchars');

        if (! empty($token) && ! empty($user_data))
        {
            $where['token'] =   $token;
            $field          =   array('user_id','expire_time','perfect_time');
            $member_data    =   M('photo_member')->where($where)->field($field)->find();

            if (empty($member_data))
            {
                $this->returnData('','please login update',101);
            }

            if ($member_data['perfect_time'] < time() || $member_data['perfect_time'] == 0 )
            {
                $user_id                  =   $member_data['user_id'];
                $user_data['update_time'] =   time();
                $user_data['perfect_time']=   time() + 7*24;
                $result     =   M('photo_member')->where(array('user_id'=>$user_id))->save($user_data); // 根据条件更新记录
            }
        }
    }
}
